package apiday07.socket;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.ServerSocket;
import java.net.Socket;
import java.nio.charset.StandardCharsets;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
 * 服务端
 */
public class Server {
    private ServerSocket serverSocket;
    private ExecutorService threadPool;

    public Server() {
        System.out.println("正在启动服务器...");
        try {
            serverSocket = new ServerSocket(8088);//端口号
            threadPool = Executors.newFixedThreadPool(30);
            System.out.println("服务器启动成功！");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public void start() {
        System.out.println("等待客户端连接服务器...");
        int count = 0;
        try {
            while (true) {
                Socket socket = serverSocket.accept();
                count++;
                System.out.println(count + "个用户已连接！");

                //方法二：
                ClientHandler handler = new ClientHandler(socket, count);
//                Thread t = new Thread(handler);
//                t.start();
                threadPool.execute(handler);


                //写法一：继承Thread，重写run()方法
                /*
                InputStream in = socket.getInputStream();
                InputStreamReader isr = new InputStreamReader(in, StandardCharsets.UTF_8);
                BufferedReader br = new BufferedReader(isr);

                new Thread("用户" + count) {
                    @Override
                    public void run() {
                        while (true) {
                            String str = null;
                            try {
                                str = br.readLine();
                            } catch (IOException e) {
                                e.printStackTrace();
                            }
                            if ("exit".equals(str)) {
                                System.out.println("用户已断开连接！");
                                break;
                            }
                            System.out.println(getName() + "说：" + str);
                        }
                    }
                }.start();*/
            }
        } catch (IOException e) {
            e.printStackTrace();
        }

    }

    public static void main(String[] args) {
        Server server = new Server();
        server.start();
    }


    //写法二：实现Runnable
    private class ClientHandler implements Runnable {
        private Socket socket;
        int count;


        public ClientHandler() {
        }

        public ClientHandler(Socket socket, int count) {
            this.socket = socket;
            this.count = count;
        }

        @Override
        public void run() {
            try {
                InputStream in = socket.getInputStream();
                InputStreamReader isr = new InputStreamReader(in, StandardCharsets.UTF_8);
                BufferedReader br = new BufferedReader(isr);
                while (true) {
                    String str;
                    if ((str = br.readLine()) != null) {
                        System.out.println("用户" + count + "说：" + str);
                    }
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}
